import {Component, OnInit, ViewContainerRef} from '@angular/core';

import {NzModalRef, NzModalService} from 'ng-zorro-antd/modal';
import {OutboundeditComponent} from '../outboundedit/outboundedit.component';
import {AdminService} from "../../../../services/admin.service";
import {AlertService} from "../../../../services/alert.service";
import {Materiainventory} from "../../../../model/inventory/Materiainventory";
import {Dbd} from "../../../../model/Dbd";
import {Rawbeaninventory} from "../../../../model/inventory/Rawbeaninventory";

@Component({
  selector: 'app-outboundlist',
  templateUrl: './outboundlist.component.html',
  styleUrls: ['./outboundlist.component.css']
})
export class OutboundlistComponent implements OnInit {
  ol:Array<Dbd>
  sl: Array<Rawbeaninventory>;
  ml: Array<Materiainventory>;
  sdsl = 0
  bcsl = 0
  constructor(private modal: NzModalService, private viewContainerRef: ViewContainerRef,
              private adminService:AdminService,private alertService:AlertService) {
    this.ol=new Array<Dbd>();

    this.sl = new Array<Rawbeaninventory>();
    this.ml = new Array<Materiainventory>();

  }

  ngOnInit(): void {
    this.initArrayData();
    this.loadMateriaInventoryStatic()
    this.loadRawBeanInventoryStastics()


  }


  initArrayData(){
    this.adminService.getDbds().then((data:any)=>{
      if(data.msg == "ok"){
        this.ol = data.list
      }else{
        this.alertService.showError("获取调拨记录失败")
      }
    })

  }

  getDbdsByClqk(id:string){
    this.adminService.getDbdsByClqk(id).then((data:any)=>{
      if(data.msg == "ok"){
        this.ol = data.list
      }else{
        this.alertService.showError("根据处理情况获取调拨记录失败")
      }
    })
  }

  outbound(i:string){
    const modalwindow:NzModalRef  = this.modal.create({
      nzTitle: '出库信息信息编辑',
      nzContent: OutboundeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        index:i
      },
      nzFooter: [

        {
          label: '确定',
          type: 'primary',
          onClick: () => this.modal.confirm({ nzTitle: '请确认出库数量', nzContent: '出库数量一经确定，无法修改' ,
            nzOnOk:()=>new Promise(resolve => {
              modalwindow.getContentComponent().save();
              setTimeout(()=>{
                this.initArrayData();
                this.loadMateriaInventoryStatic();
                this.loadRawBeanInventoryStastics();
                resolve(true);


                //this.getEmpDetailByEmpid(this.empid);
                //this.loadEmpImg();
              }, 1000)
            })
          })
        },

        {
          label: '关闭',
          type: 'dashed',
          onClick: () =>modalwindow.destroy()
        }
      ]

    })
  }


  loadRawBeanInventoryStastics() {
    this.adminService.getRawBeanNum()
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.sl = new Array<Rawbeaninventory>();
          this.sl = data.list;
          //计算总数
          this.sdsl = 0;
          for (let i = 0; i < data.list.length; i++) {
            this.sdsl = this.sdsl + data.list[i].total;
          }
        }
      })
  }
  loadMateriaInventoryStatic() {
    this.adminService.getMateriaStaticNum()
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.ml = new Array<Materiainventory>();
          this.ml = data.list;
          this.bcsl = 0;
          for (let i = 0; i < data.list.length; i++) {
            this.bcsl = this.bcsl + data.list[i].total;
          }
        }
        else if (data.msg == 'empty') {
          this.ml = new Array<Materiainventory>();
        }
        else {
          this.alertService.showError('数据获取异常，请重试');
        }
      })
  }
}
